/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

/*
    SPDX-FileCopyrightText: 2024 Key <xiehui@nfschina.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

import QtQuick 2.15
import QtQuick.Controls 2.15
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.extras 2.0 as PlasmaExtras

import org.kde.plasma.private.kicker 0.1 as Kicker

Item {
    id: girdViewStack
    property int margins: 0
    // property var model: null
    property int gridRowCount: 7 // 每行数量
    property int gridColumnCount: 4 // 每列数量
    property var dataModel: null
    property var pageModel: girdViewStack.dataModel.pageModelForRow(0)
    function turningPage(isDown) {
        // 如果当前在进行动画，代表当前处于翻页中，这个时候不应该让用户进行翻页
        if (dataStack.busy) {
            return;
        }
        if (!isDown && dataStack.depth > 1) {
            dataStack.pop()
        } else if (isDown && dataStack.depth < dataModel.pageCount) {
            dataStack.push(gridViewPage.createObject(dataStack))
        }
        girdViewStack.pageModel = dataModel.pageModelForRow(dataStack.depth - 1)

    }
    Connections {
        target: dataModel
        function onPageCountChanged() {
            girdViewStack.pageModel = dataModel.pageModelForRow(0)
        }
    }
    Component.onCompleted: {
        girdViewStack.pageModel = dataModel.pageModelForRow(0)
    }

    StackView {
        id: dataStack
        anchors {
            fill: parent
            margins: girdViewStack.margins
        }

       // 定义返回上一页时的动画
        popEnter: Transition {
            XAnimator {
                from: -dataStack.width
                to: 0
                duration: 300
                easing.type: Easing.InOutQuad
            }
        }
        popExit: Transition {
            XAnimator {
                from: -dataStack.width
                to: 0
                duration: 300
                easing.type: Easing.InOutQuad
            }
        }
    
        // 定义进入新页面时的动画
        pushEnter: Transition {
            XAnimator {
                from: dataStack.width
                to: 0
                duration: 300
                easing.type: Easing.InOutQuad
            }
        }
        pushExit: Transition {
            XAnimator {
                from: dataStack.width
                to: 0
                duration: 300
                easing.type: Easing.InOutQuad
            }
        }
        /*** 
            这里不能用initialItem, 会导致第一次进入时，数据不能正确显示，
            只显示了在创建过程中创建的数据，可能是1个，可能是几个，
            所以这里用了Component.onCompleted来创建。
        ***/
        Component.onCompleted : {
            dataStack.push(gridViewPage.createObject(dataStack))
        } 
    }

    Rectangle {
        id: leftAndRight
        width: parent.width
        anchors {
            top: parent.bottom
            topMargin: -90 * root.scaleFactor
        }
        color: "transparent"
        Row {
            anchors{
                horizontalCenter: parent.horizontalCenter
                top: parent.top
                topMargin: 20 * root.scaleFactor
            }
            spacing: 20
            Repeater {
                model: dataModel.pageCount
                Rectangle {
                    width: dataStack.depth - 1 == index ? 10 * root.scaleFactor : 5 * root.scaleFactor
                    y: dataStack.depth - 1 == index ? 0 : width / 2
                    height: width
                    radius: width / 2
                    color: "#333333"
                }
            }
        }
        height: 90 * root.scaleFactor
        MouseArea {
            anchors.fill: parent
            onClicked: (mouse)=> {
                girdViewStack.turningPage(mouse.x > parent.width / 2)
            }
        }
    }

    Component {
        id: gridViewPage
        Item {
            id: gridItem
            anchors {
                fill: parent
            }

            clip: true
            ItemGridView {
                id: gridView
                anchors.top: parent.top
                width: parent.width
                height: parent.height
                cellWidth: width / girdViewStack.gridRowCount - 2
                cellHeight:  height / girdViewStack.gridColumnCount
                iconSize: root.iconSize
                model: girdViewStack.pageModel
            }
        }
    }
}